<?php
/**
 * Created by PhpStorm.
 * User: yuyj
 * Date: 17-1-3
 * Time: 下午3:36
 */

namespace common\services\outstock;


use common\entities\myhtsq\ErpUserEntity;
use common\entities\outstock\MyscrmOutStockOrderEntity;
use common\entities\outstock\MyscrmOutStockStepEntity;
use common\repositories\myhtsq\ErpBusinessUnitRepository;
use common\repositories\outstock\OutStockStepRepository;
use common\services\BaseService;
use common\services\myhtsq\ErpBusinessUnitService;
use common\services\myhtsq\ErpUserService;
use Yii;
use yii\base\Model;
use yii\helpers\ArrayHelper;

class OutStockStepService extends BaseService
{
    private $repository;

    /**
     * @inheritdoc
     */
    public function __construct(OutStockStepRepository $repository, $config = [])
    {
        $this->repository = $repository;

        return parent::__construct($config);
    }

    /**
     * @inheritdoc
     */
    public function create(MyscrmOutStockStepEntity $model)
    {
        return $this->repository->create($model);
    }

    /**
     * @param $orderId
     * @return ErpUserEntity
     */
    public function getLatestSpUser($orderId)
    {
        $step = $this->repository->getLatestSpStep($orderId);

        if (empty($step)) {
            return self::setAndReturn(__LINE__, '找不到最后审批动作');
        }

        /** @var ErpUserService $service */
        $service = Yii::createObject(ErpUserService::className());
        $data = $service->getUserById($step['operate_people_id']);

        if (empty($data)) {
            return self::setAndReturn(__LINE__, '找不到最后审批用户');
        }

        return $data;
    }

    public function getStepList($orderId)
    {
        /** @var OutStockOrderService $service */
        $service = Yii::createObject(OutStockOrderService::className());
        $order = $service->getOrderById($orderId);

        if (empty($order)) {
            return self::setAndReturn(__LINE__, '单据' . $orderId . '不存在');
        }

        $data = $this->repository->getStepList($order->id);

        $data = $this->paddingActionResultAndSuggestion($order, $data);
        $data = $this->paddingDepartmentName($data);

        return $data;
    }

    /**
     * 填充动作的结果与意见
     * @param MyscrmOutStockOrderEntity $order
     * @param                           $data
     * @return mixed
     */
    private function paddingActionResultAndSuggestion(MyscrmOutStockOrderEntity $order, $data)
    {
        foreach ($data as &$item) {
            if ($item instanceof Model) {
                $item = $item->toArray();
            }

            //动作：1发起，10打回，11撤回，20审批，21确认
            switch ($item['action']) {
                case OutStockFlowService::ACTION_FQ:
                    $item['result'] = '发起';
                    $item['suggestion'] = '发起说明：无';
                    break;
                case OutStockFlowService::ACTION_CH:
                    $item['result'] = '撤回';
                    $item['suggestion'] = '撤回说明：' . ($item['remarks'] ?  $item['remarks'] : '无');
                    break;
                case OutStockFlowService::ACTION_DH:
                    $item['result'] = '审批不通过';
                    $item['suggestion'] = '审批意见：不同意' . ($item['remarks'] ? '，' . $item['remarks'] : '');
                    break;
                case OutStockFlowService::ACTION_SP:
                    $item['result'] = '审批通过';
                    $item['suggestion'] = '审批意见：同意';
                    break;
                case OutStockFlowService::ACTION_QR:
                    $item['result'] = '审批通过';
                    $item['suggestion'] = '审批意见：同意';
                    break;
            }

            $item['action'] = OutStockFlowService::$actionMap[$item['action']];
        }

        return $data;
    }

    /**
     * 填充部门名称
     * @param $data
     * @return mixed
     */
    private
    function paddingDepartmentName($data)
    {
        $departmentIds = ArrayHelper::getColumn($data, 'operate_people_department_id');
        /** @var ErpBusinessUnitService $service */
        $service = Yii::createObject(ErpBusinessUnitService::className());
        $departments = $service->getDepartmentNameByDepartmentId($departmentIds);

        foreach ($data as &$item) {
            if ($item instanceof Model) {
                $item = $item->toArray();
            }

            if (!isset($departments[$item['operate_people_department_id']])) {
                $departments[$item['operate_people_department_id']] = '';
            }
            $item['department_name'] = $departments[$item['operate_people_department_id']];
        }

        return $data;
    }

    public
    function getLatestFqStep($orderId)
    {
        return $this->repository->getLatestFqStep($orderId);
    }
}